#ifndef TESTMODEL_H
#define TESTMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QQmlListProperty>
#include <QJsonObject>

class TestItem: public QObject{
    Q_OBJECT
    Q_PROPERTY(QString name WRITE setName READ getName NOTIFY nameChanged)
    Q_PROPERTY(int  value WRITE setValue READ getValue NOTIFY valueChanged)
    public:
    //using QObject::QObject;
    using QObject::QObject;
public:
    void setName(const QString& );
    QString getName();
    void setValue(int );
    int getValue();




    ~TestItem(){

    }
signals:
    void nameChanged();
    void valueChanged();
private:
    QString name_;
    int value_;

};


class TestModel: public QAbstractListModel{

    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<TestItem> datas READ datas  NOTIFY nameChanged)
    enum Role{
        Name = Qt::DisplayRole + 1,
        Value
    };

signals:
    void nameChanged();
public:
    using QAbstractListModel::QAbstractListModel;
    using TestItemListType = QList<TestItem *>;

    QHash<int , QByteArray> roleNames()const override;
    QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole ) const override;
    int rowCount(const QModelIndex &parien = QModelIndex()) const override;




    Q_INVOKABLE void append(const QJsonObject & item);
    QQmlListProperty<TestItem> datas();



    static  void AppendFunction(QQmlListProperty<TestItem> *,TestItem *);
    static  int CountFunction(QQmlListProperty<TestItem> *);
    static  TestItem * AtFunction(QQmlListProperty<TestItem> *,int);
    static  void  ClearFunction(QQmlListProperty<TestItem> *);

    TestModel();
private:


    QList<TestItem *> items_;
    mutable QHash<int , QByteArray> roles_;


};









#endif // TESTMODEL_H
